package csc.fresher.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.dao.CustomerDao;
import csc.fresher.dao.InternetAccountDAO;
import csc.fresher.dao.SavingAccountDao;
import csc.fresher.dao.DepositAccountDao;
import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.InternetAccount;



@ManagedBean
@SessionScoped
public class CustomerManageBean implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
        private Customer cus;
    	private List<CustomerAccount> sav = new ArrayList<CustomerAccount>();
    	private List<CustomerAccount> dep = new ArrayList<CustomerAccount>();
    	private boolean checkDep = false;
    	private CustomerAccount depToShow;
    	
		public CustomerAccount getDepToShow() {
			return depToShow;
		}

		public void setDepToShow(CustomerAccount depToShow) {
			this.depToShow = depToShow;
		}

		public boolean isCheckDep() {
			return checkDep;
		}

		public void setCheckDep(boolean checkDep) {
			this.checkDep = checkDep;
		}

		@ManagedProperty(value="#{customerDaoImpl}")
        CustomerDao customerDao;

        @ManagedProperty(value="#{savingAccountDaoImpl}")
        SavingAccountDao savingDao;

		@ManagedProperty(value="#{depositAccountDaoImpl}")
        DepositAccountDao depositDao;
        
        @ManagedProperty(value="#{internetAccountDAOImpl}")
        InternetAccountDAO internetAcountDAO;
        
        @ManagedProperty(value="#{internetLoginManageBean}")
        private InternetLoginManageBean cmb;
        
        @ManagedProperty(value="#{internetTransactionManageBean}")
        private InternetTransactionManageBean internetTransaction;

		public void setCustomerDao(CustomerDao customerDao) {
                this.customerDao = customerDao;
        }
        
        public void setSavingDao(SavingAccountDao savingDao) {
			this.savingDao = savingDao;
		}

		public void setDepositDao(DepositAccountDao depositDao) {
			this.depositDao = depositDao;
		}

        public void setInternetAcountDAO(InternetAccountDAO internetAcountDAO) {
                this.internetAcountDAO = internetAcountDAO;
        }
        
        public List<CustomerAccount> getSav() {
			return sav;
		}

		public void setSav(List<CustomerAccount> sav) {
			this.sav = sav;
		}

		public List<CustomerAccount> getDep() {
			return dep;
		}

		public void setDep(List<CustomerAccount> dep) {
			this.dep = dep;
		}
        
        public Customer getCus() {
                return cus;
        }


        public void setCus(Customer cus) {
                this.cus = cus;
        }


        public InternetLoginManageBean getCmb() {
                return cmb;
        }


        public void setCmb(InternetLoginManageBean cmb) {
                this.cmb = cmb;
        }
        
    	private List<CustomerAccount> listAccount;
    	public List<CustomerAccount> getListAccount() {
    		return listAccount;
    	}

    	public void setListAccount(List<CustomerAccount> listAccount) {
    		this.listAccount = listAccount;
    	}

    	public String transferMoney() {
    		listAccount = depositDao.getAccountByIDCardNumber(cus.getIdCardNumber());
    		return "transfer";
    	}
       
    	@PostConstruct
    	public void init(){
    		InternetAccount internetAcc = internetAcountDAO.getInternetAccountByUsername(cmb.getAcc().getUsername());
            cus = customerDao.getCustomerByID(internetAcc.getCustomerID());
            cus.setPhoneNumber(cus.getPhoneNumber().substring(cus.getPhoneNumber().length() -4));
            cus.setPhoneNumber2(cus.getPhoneNumber2().substring(cus.getPhoneNumber2().length() -4));
            sav = savingDao.getListCutomerAccountsbyCardNumberID(cus.getIdCardNumber());
            dep = depositDao.getAccountByIDCardNumber(cus.getIdCardNumber());
            depToShow = dep.get(0);
    	}

        public String showCustomerDetail()
        {
                return "accountdetail";
        }
        
        public String showDepositAccountTransaction(Long depID){       
        	for (int i=0; i<dep.size(); i++){
        		if (dep.get(i).getAccountNumber()==depID)  depToShow = dep.get(i);
        		checkDep = true;
        	}
        	internetTransaction.setAccountNumber(depID);
        	return "transactiondetail";
        	
        }
        
        public InternetTransactionManageBean getInternetTransaction() {
			return internetTransaction;
		}

		public void setInternetTransaction(
				InternetTransactionManageBean internetTransaction) {
			this.internetTransaction = internetTransaction;
		}

		public void show(AjaxBehaviorEvent e){       
        	long depIdToShow = internetTransaction.getAccountNumber();
        	for (int i=0; i<dep.size(); i++){
        		if (dep.get(i).getAccountNumber()==depIdToShow) depToShow = dep.get(i);
        		checkDep = true;
        	}
        	internetTransaction.setTransactionList(null);
        	
        }
}